<?php

use GatewayWorker\Lib\Db;
use GatewayWorker\Lib\Gateway;


class Events
{
    // 当有客户端连接时，将client_id返回，让mvc框架判断当前uid并执行绑定
    public static function onConnect($client_id)
    {

    }

    public static function onMessage($client_id, $message)
    {
//        Gateway::sendToClient($client_id, "BBB --- ".$message);
//        return;

        try {
            $db1 = Db::instance('db1');
            $message = str_replace(array("\r\n", "\r", "\n"), "", $message);
            $insert_id = $db1->insert('test')->cols(array('name' => "client_id=" . $client_id . " --- FG_onMessage=" . $message))->query();

            if (strncmp($message, ">", 1) == 0) {
//                >HF-THINKER_71D4B6,CR04,0003*
//                >AI-THINKER_71D4B6,FE01,0100*
                $v = explode("*", $message);
                if (count($v) > 1) {
                    $value = $v[0];
                    $v1 = explode(",", $v[0]);
                    if (count($v1) > 2) {
                        $deviceid = str_replace(">", '', $v1[0]);
                        $command = $v1[1];
                        $content = $v1[2];

                        $id = $db1->select('id')
                            ->from('fa_fg_device')
                            ->where('deviceid= :deviceid')
                            ->bindValues(array('deviceid' => $deviceid))
                            ->single();

                        $data = array(
                            'client_id' => $client_id,
                            'isonline' => 1,
                            'update_time' => date('Y-m-d H:i:s', time())
                        );

                        //>设备号，LF/HF，内容，0-失败 1-成功，0000-COD
                        $issuccess = intval($v1[3]);
                        $type = $v1[4];
                        //0-讀取, 1-寫入
                        $state = 0;
                        $msg = "";
                        if ($command == "CR00") {
                            //顯示LF UID
                            //>LF-THINKER_71D4B6,CR00,F1A0BF80*
                            $data['LF_UID'] = $content;
                            $msg = "LF UID 讀取 " . $content;
                        } else if ($command == "CR01") {
                            //顯示LF COD
                            //>LF-THINKER_71D4B6,CR01,FFBDF84DEF84A940*
                            $data['LF_COD'] = $content;
                            $msg = "LF COD 讀取 " . $data['LF_COD'];
                        } else if ($command == "CR02") {
                            //顯示UHF TID
                            //>HF-THINKER_71D4B6,CR02,E2 80 11 0C 20 00 7A CD F6 50 09 9C *
                            $data['UHF_TID'] = $content;
                            $msg = "UHF TID 讀取 " . $content;
                        } else if ($command == "CR03") {
                            //顯示 UHF EPC
                            //>HF-THINKER_71D4B6,CR03,00C4BBC455*
                            $data['UHF_EPC'] = $content;
                            $msg = "UHF EPC 讀取 " . $content;
                        } else if ($command == "CR05") {
                            //寫入
                            if ($type == "0000") {
                                $type = "LF COD";
                            } else {
                                $type = "UHF EPC";// 0001
                            }
                            $s = "失敗";
                            if ($issuccess == 1) {
                                $s = "成功";
                            }
                            //寫LF COD(Page2,3,5)失敗
                            //>LF-THINKER_71D4B6,CR05,F1A0BF80,0,0000*
                            $msg = $type . " 寫入 [" . $content . "] " . $s;
                        }

                        if (empty($id)) {
                            $data['deviceid'] = $deviceid;
                            $db1->insert('fa_fg_device')->cols($data)->query();
                        } else {
                            $db1->update('fa_fg_device')->cols($data)->where("id=" . $id)->query();
                        }

                        $data = array(
                            'deviceid' => $deviceid,
                            'command' => $command,
                            'content' => $content,
                            'msg' => $msg,
                            'state' => $state
                        );
                        $db1->insert('fa_fg_records')->cols($data)->query();

                        if ($command == 'CR02') {
                            $id = $db1->select('id')
                                ->from('fa_fg_ringrecords')
                                ->where('tid= :tid')
                                ->bindValues(array('tid' => $content))
                                ->single();

                            $data = array(
                                'deviceid' => $deviceid,
                                'tid' => $content,
                                'epc' => '',
                            );

                            if (empty($id)) {
                                $db1->insert('fa_fg_ringrecords')->cols($data)->query();
                            } else {
                                $db1->update('fa_fg_ringrecords')->cols($data)->where("id=" . $id)->query();
                            }
                        }

                        Gateway::sendToClient($client_id, "OK");
                    }
                }
            }


        } catch (\Exception $e) {
            exit(250);
        } catch (\Error $e) {
            exit(250);
        }

    }

    public static function onClose($client_id)
    {

    }


}
